package com.tcm.doctor.web.controller;

import com.base.core.utils.R;
import com.tcm.doctor.domain.vo.ServicePriceVo;
import com.tcm.doctor.service.PrescribePriceService;
import com.tcm.prescribe.api.domain.vo.ServicePriceOptionVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


@RequestMapping("/doctor")
@RestController
public class PrescribePriceController {

    @DubboReference(version = "0.0.1-alpha", timeout = 6000, check = false, retries = 3)
    private PrescribePriceService prescribePriceService;



    @GetMapping("/service-price")
    public R servicePrice(@RequestParam("doctorUserId") Long doctorUserId) {
        try{
            List<ServicePriceVo> servicePriceVos = prescribePriceService.servicePrice(doctorUserId);
            List<ServicePriceOptionVo> servicePriceOptionVos = new ArrayList<>();
            if(servicePriceVos != null && !servicePriceVos.isEmpty()){
                for(ServicePriceVo servicePriceVo : servicePriceVos){
                    ServicePriceOptionVo servicePriceOptionVo = new ServicePriceOptionVo();
                    servicePriceOptionVo.setText(servicePriceVo.getTitle());
                    servicePriceOptionVo.setValue(servicePriceVo.getServicePrice().toString());
                    servicePriceOptionVos.add(servicePriceOptionVo);
                }
            }
            return R.success(servicePriceOptionVos);
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }


    /**
     * 根据药价获得服务费
     * @param drugsPrice
     * @return
     */
    @GetMapping("/default-service-price")
    public R defaultServicePrice(@RequestParam("drugsPrice") BigDecimal drugsPrice,
                                 @RequestParam("doctorUserId") Long doctorUserId){
        try{
            return R.success(prescribePriceService.defaultServicePrice(drugsPrice, doctorUserId));
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }
}
